package bwTest
{
	import flash.events.Event;
	import flash.net.Responder;
	import flash.system.System;

	public class BWCheck
	{
		var nc:NetConnection2 = new NetConnection2();
		private var res:Object;	
		private	var payload:Array = new Array();

		
		public function BWCheck(url:String)
		{
			trace("BWCheck");
			nc.nc.client = new NetConnectionBWClient();
			nc.dispatcher.addEventListener(NetConnection2.CONNECTED_EVENT, connected);
			nc.url = url;
			//nc.connect();
		}
		
		public function connected(e:Event):void {
			trace("BWCheck connected");
			nc.nc.call("onClientBWCheck", new Responder(getBWCheckData, noBWData), null);
		}
		
		
		public function getBWCheckData(p_res:Object):void {
			trace ("ClientBWResult: ");
			var now:Number = (new Date()).getTime()/1;
			if(res.sent == 0) {
				res.beginningValues = p_res;
				res.beginningValues.time = now;
				res.pakSent[res.sent++] = now;
				nc.nc.call("onClientBWCheck", new Responder(getBWCheckData, noBWData), now);
			} else {
				res.pakRecv[res.count] = now;
				trace( "Packet interval = " + (res.pakRecv[res.count] - res.pakSent[res.count])*1  );
				res.count++;
				var timePassed:Number = (now - res.beginningValues.time);
				
				if (res.count == 1) {
					res.latency = Math.min(timePassed, 800);
					res.latency = Math.max(res.latency, 10);
					res.overhead = p_res.cOutBytes - res.beginningValues.cOutBytes;
					trace("overhead: "+res.overhead);
					res.pakSent[res.sent++] = now;
					nc.nc.call("onClientBWCheck", new Responder(getBWCheckData, noBWData), now, payload);
				}
				trace("count: "+res.count+ " sent: "+res.sent+" timePassed: "+timePassed+" latency: "+res.latency);
				
				// If we have a hi-speed network with low latency send more to determine
				// better bandwidth numbers, send no more than 6 packets
				if ( (res.count >= 1) && (timePassed<1000))
				{
					res.pakSent[res.sent++] = now;
					res.cumLatency++;
					nc.nc.call("onClientBWCheck", new Responder(getBWCheckData, noBWData), now, payload);
				} else if ( res.sent == res.count ) {	
					// See if we need to normalize latency
					if ( res.latency >= 100 )
					{ // make sure we detect sattelite and modem correctly
						if (  res.pakRecv[1] - res.pakRecv[0] > 1000 )
						{
							res.latency = 100;
						}
					}
					payload = null;
					System.gc();
					//delete payload;
					// Got back responses for all the packets compute the bandwidth.
					var stats:Object = p_res;
					var deltaUp:Number = (stats.cOutBytes - res.beginningValues.cOutBytes)*8/1000;
					var deltaTime:Number = ((now - res.beginningValues.time) - (res.latency * res.cumLatency) )/1000;
					if ( deltaTime <= 0 )
						deltaTime = (now - res.beginningValues.time)/1000;
					
					var kbitUp:Number = Math.round(deltaUp/deltaTime);
					
					trace("getBWCheckData: kbitUp = " + kbitUp + ", deltaUp= " + deltaUp + ", deltaTime = " + deltaTime + ", latency = " + res.latency + " KBytes " + (stats.cOutBytes - res.beginningValues.cOutBytes)/1024) ;
				}
			}
			
		}
		public function onBWDone(kbitDown:Number, deltaDown:Number, deltaTime:Number, latency:Number):void
		{
			trace("onBWDone: kbitDown:"+kbitDown+" deltaDown:"+deltaDown+" deltaTime:"+deltaTime+" latency:"+latency);
			// app logic based on the bandwidth detected follows here
			var detected_bw:Number = kbitDown;
			// close the Netconnection to bwcheck
		}
		private function noBWData(result:Object):void{
			trace("No BandWidth data: ", result);
		}	
		
		
		
		
	}
}